﻿package com.sh.game.util
{
    import flash.display.*;
    import flash.geom.*;

    public class BitmapScale9Grid extends Sprite
    {
        private var source:Bitmap;
        private var scaleGridTop:Number;
        private var scaleGridBottom:Number;
        private var scaleGridLeft:Number;
        private var scaleGridRight:Number;
        private var leftUp:Bitmap;
        private var leftCenter:Bitmap;
        private var leftBottom:Bitmap;
        private var centerUp:Bitmap;
        private var center:Bitmap;
        private var centerBottom:Bitmap;
        private var rightUp:Bitmap;
        private var rightCenter:Bitmap;
        private var rightBottom:Bitmap;
        private var _width:Number;
        private var _height:Number;
        private var minWidth:Number;
        private var minHeight:Number;

        public function BitmapScale9Grid(param1:Bitmap, param2:Number, param3:Number, param4:Number, param5:Number)
        {
            this.source = param1;
            this.scaleGridTop = param2;
            this.scaleGridBottom = param3;
            this.scaleGridLeft = param4;
            this.scaleGridRight = param5;
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._width = this.source.width;
            this._height = this.source.height;
            this.leftUp = this.getBitmap(0, 0, this.scaleGridLeft, this.scaleGridTop);
            this.addChild(this.leftUp);
            this.leftCenter = this.getBitmap(0, this.scaleGridTop, this.scaleGridLeft, this.scaleGridBottom - this.scaleGridTop);
            this.addChild(this.leftCenter);
            this.leftBottom = this.getBitmap(0, this.scaleGridBottom, this.scaleGridLeft, this.source.height - this.scaleGridBottom);
            this.addChild(this.leftBottom);
            this.centerUp = this.getBitmap(this.scaleGridLeft, 0, this.scaleGridRight - this.scaleGridLeft, this.scaleGridTop);
            this.addChild(this.centerUp);
            this.center = this.getBitmap(this.scaleGridLeft, this.scaleGridTop, this.scaleGridRight - this.scaleGridLeft, this.scaleGridBottom - this.scaleGridTop);
            this.addChild(this.center);
            this.centerBottom = this.getBitmap(this.scaleGridLeft, this.scaleGridBottom, this.scaleGridRight - this.scaleGridLeft, this.source.height - this.scaleGridBottom);
            this.addChild(this.centerBottom);
            this.rightUp = this.getBitmap(this.scaleGridRight, 0, this.source.width - this.scaleGridRight, this.scaleGridTop);
            this.addChild(this.rightUp);
            this.rightCenter = this.getBitmap(this.scaleGridRight, this.scaleGridTop, this.source.width - this.scaleGridRight, this.scaleGridBottom - this.scaleGridTop);
            this.addChild(this.rightCenter);
            this.rightBottom = this.getBitmap(this.scaleGridRight, this.scaleGridBottom, this.source.width - this.scaleGridRight, this.source.height - this.scaleGridBottom);
            this.addChild(this.rightBottom);
            this.minWidth = this.leftUp.width + this.rightBottom.width;
            this.minHeight = this.leftBottom.height + this.rightBottom.height;
            return;
        }// end function

        private function getBitmap(param1:Number, param2:Number, param3:Number, param4:Number) : Bitmap
        {
            var _loc_5:* = new BitmapData(param3, param4);
            new BitmapData(param3, param4).copyPixels(this.source.bitmapData, new Rectangle(param1, param2, param3, param4), new Point(0, 0));
            var _loc_6:* = new Bitmap(_loc_5);
            new Bitmap(_loc_5).x = param1;
            _loc_6.y = param2;
            return _loc_6;
        }// end function

        override public function set width(param1:Number) : void
        {
            if (param1 < this.minWidth)
            {
                param1 = this.minWidth;
            }
            this._width = param1;
            this.refurbishSize();
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            if (param1 < this.minHeight)
            {
                param1 = this.minHeight;
            }
            this._height = param1;
            this.refurbishSize();
            return;
        }// end function

        private function refurbishSize() : void
        {
            this.leftCenter.height = this._height - this.leftUp.height - this.leftBottom.height;
            this.leftBottom.y = this._height - this.leftBottom.height;
            this.centerUp.width = this._width - this.leftUp.width - this.rightUp.width;
            this.center.width = this.centerUp.width;
            this.center.height = this.leftCenter.height;
            this.centerBottom.width = this.center.width;
            this.centerBottom.y = this.leftBottom.y;
            this.rightUp.x = this._width - this.rightUp.width;
            this.rightCenter.x = this.rightUp.x;
            this.rightCenter.height = this.center.height;
            this.rightBottom.x = this.rightUp.x;
            this.rightBottom.y = this.leftBottom.y;
            return;
        }// end function

    }
}
